#include "../../head/utils/MutiSerialControl.h"
#include "../../head/utils/DemoduleUtil.h"
#include <QtDebug>
#include<QtSerialPort/QSerialPort>      //串口功能头文件
#include<QByteArray>
#include<QTimer>
#include<QString>
#include<QMessageBox>
#include<QCoreApplication>
#include <QTime>


MutiSerialControl::MutiSerialControl(QObject *parent):QObject(parent)
{

}

void MutiSerialControl::init_Serial(int PreSignalLen){
    FragmentLen = PreSignalLen * 6;
    SerialTotalLen = FragmentLen * 10;
    sendDecDataPort = new QSerialPort(this);
    for(int i = 0 ; i < SERIAL_PORT_COUNT ; i++){
        //设置串口属性
        serialPort[i] = new QSerialPort(this);
        serialPort[i]->setReadBufferSize(SerialTotalLen);


        fifoReceiver1[i].Buf = (double*)malloc(SerialTotalLen*sizeof(double));
        memset(fifoReceiver1[i].Buf,0,SerialTotalLen*sizeof(double));
        fifoReceiver1[i].nWp = 0;
        fifoReceiver1[i].nRp = 0;
        fifoReceiver1[i].nUsedCnt = 0;
        fifoReceiver1[i].deep = SerialTotalLen;

        timer[i] = new QTimer(this);



        connect(timer[i],&QTimer::timeout,this, [=](){

            // qDebug() << "timeout \n" << endl;
            /************************************************
         * 使用长度超过接收BUFFER的总长度，串口出现问题点
         * 1、接收的数据格式不正确；
         * 2、串口自身出现问题；需要重置；
         * 3、图形渲染与串口阻塞；导致渲染时间过长；
         * *********************************************/

            if(serialPort[i]->isOpen()){
                QByteArray array = serialPort[i]->readAll();

                int readnb = array.size();

                if(readnb > 0){
                    for (int z = 0; z < readnb; z++){
                        fifoReceiver1[i].Buf[fifoReceiver1[i].nWp] = (uint8_t)array[z];
                        //qDebug()<<"array[z]"<<(uint8_t)array[z]<<endl;
                        fifoReceiver1[i].nUsedCnt++;
                        if(fifoReceiver1[i].nWp < SerialTotalLen-1){
                            fifoReceiver1[i].nWp++;
                        }else{
                            fifoReceiver1[i].nWp=0;
                        }
                    }
                }

                if(find_start_lable[i] == 0){
                    for(int j = 0 ; j < 6 ; j ++){
                        if ((fifoReceiver1[i].Buf[j] == 245) && (fifoReceiver1[i].Buf[j+1]== 245)){ //F5 F5
                            find_start_lable[i]=1;
                            fifoReceiver1[i].nRp=j+2;
                            fifoReceiver1[i].nUsedCnt=fifoReceiver1[i].nUsedCnt-2;
                            qDebug() << "SCICOM ok, \n" << endl;
                            break;
                        }else{
                            qDebug() << "SCICOM error3, \n" << endl;
                        }

                    }
                }
                if(find_start_lable[i] == 1 && fifoReceiver1[i].nUsedCnt >= FragmentLen){
                  // qDebug()<<"i===="<<i<<"---time---"<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz")<<endl;
                   //QDateTime curDateTime = QDateTime::currentDateTime();

                   emit sendToDemoudle(i);
                }
            }
        });

    }
}


void MutiSerialControl::stop_Serial(int hzType){
    timer[hzType]->stop();
}

void MutiSerialControl::start_Serial(int hzType){
    timer[hzType]->start(400);
}


void MutiSerialControl::reset_Serial_Param(int hzType){
    memset(fifoReceiver1[hzType].Buf,0,SerialTotalLen*sizeof(double));
    fifoReceiver1[hzType].nWp = 0;
    fifoReceiver1[hzType].nRp = 0;
    fifoReceiver1[hzType].nUsedCnt = 0;
    fifoReceiver1[hzType].deep = SerialTotalLen;
    find_start_lable[hzType] = 0;
    //demoduleUtil->record_log("SCICOM reset..." );
    //qDebug() << "SCICOM reset..." << endl;
    serialPort[hzType]->close();
    serialPort[hzType]->open(QIODevice::ReadOnly);
    //qDebug() << "SCICOM reset ok..." << endl;
    //demoduleUtil->record_log("SCICOM reset ok..." );
}
